#coding=utf-8
'''
Created on 2013年11月12日

@author: xuben
主城管理
'''

from ..common import BasicThread, Logger
from ..role import FunctionData, RoleData
import Maincity
import MaincityData
import time

maincityManager = None
maincityGetInfoRequest = None
resourceRegionGetInfoRequest = None
politicalEventGetInfoRequest = None

#当前处理的资源区编号
currentResourceRegionId = 0
#当前处理的建筑编号(非真实编号,只是在数组中的位置)
currentBuildingId = 0
#当前处理的建筑真实编号
currentBuildingRealId = 0
#是否正在处理政务事件
isDealingPoliticalEvent = False

def maincityGetInfoCallback():
    Logger.log('成功获取主城信息', output = True)

def resourceRegionGetInfoCheck():
    global currentResourceRegionId
    #正在处理政务事件
    if isDealingPoliticalEvent:
        return False
    #查看当前区域是否有政务事件
    regionId = currentResourceRegionId + 1
    if regionId in MaincityData.resourceRegionRoughList:
        roughVO = MaincityData.resourceRegionRoughList[regionId]
        #有政务事件需要处理
        if roughVO.isOpen and roughVO.eventNum > 0:
            Logger.log('资源区发现政务事件，id:' + str(regionId), output = True)
            resourceRegionGetInfoRequest.params = 'type='+str(regionId)
            return True
    
    #资源区数量
    resourceRegionNum = len(MaincityData.resourceRegionRoughList)
    #更新当前资源区编号
    if resourceRegionNum > 0:
        currentResourceRegionId = regionId % resourceRegionNum
    return False

def resourceRegionGetInfoCallback():
    global isDealingPoliticalEvent
    global currentBuildingId
    global currentResourceRegionId
    #更新当前资源区编号
    resourceRegionNum = len(MaincityData.resourceRegionRoughList)
    currentResourceRegionId = (currentResourceRegionId+1) % resourceRegionNum
    #察看是否有政务事件
    if MaincityData.resourceRegionDetailVO:
        for buildingVO in MaincityData.resourceRegionDetailVO.buildingList:
            if buildingVO.hasEvent:
                isDealingPoliticalEvent = True
                currentBuildingId = 0
                break

def politicalEventGetInfoCheck():
    global currentBuildingId
    global currentBuildingRealId
    global isDealingPoliticalEvent
    #当前不在处理政务事件
    if not isDealingPoliticalEvent:
        return False
    #获取下一个可处理的政务事件
    detailVO = MaincityData.resourceRegionDetailVO
    if detailVO:
        while currentBuildingId < len(detailVO.buildingList):
            buildingVO = detailVO.buildingList[currentBuildingId]
            if buildingVO.hasEvent:
                #其实在callback那里设置似乎更好
                buildingVO.hasEvent = False
                currentBuildingRealId = buildingVO.id
                Logger.log('准备处理政务事件:'+buildingVO.name, output=True)
                politicalEventGetInfoRequest.params = 'buildingId=' + str(buildingVO.id)
                return True
            else:
                currentBuildingId += 1
    #当前区域已经没有可处理的政务事件
    isDealingPoliticalEvent = False
    return False

def politicalEventGetInfoCallback():
    politicalEventVO = MaincityData.politicalEventVO
    if politicalEventVO:
        #获取选项类型
        type1 = politicalEventVO.type1
        type2 = politicalEventVO.type2
        Logger.log('政务事件选项1类型:'+str(type1), output=True)
        Logger.log('政务事件选项2类型:'+str(type2), output=True)
        #暂时只处理无民忠的政务事件
        if type1 == RoleData.RES_TYPE_LOYALTY or type2 == RoleData.RES_TYPE_LOYALTY:
            return
        #获取资源的优先级
        priorityList = RoleData.getResourcePriority()
        priority1 = len(priorityList) if type1 not in priorityList else priorityList[type1]
        priority2 = len(priorityList) if type2 not in priorityList else priorityList[type2]
        #优先选择不消耗金币的
        choice = 1
        if politicalEventVO.goldConsume1 > 0 or priority1 > priority2:
            choice = 2
        Maincity.dealPoliticalEvent(currentBuildingRealId, choice)    
    #更新当前处理的建筑编号
    global currentBuildingId
    currentBuildingId += 1

def start():
    global maincityManager
    global maincityGetInfoRequest
    global resourceRegionGetInfoRequest
    global politicalEventGetInfoRequest
    if maincityManager == None:
        maincityManager = MaincityManager(name="maincityManager")
    maincityGetInfoRequest = Maincity.MaincityGetInfoRequest(None, maincityGetInfoCallback, 30, 5)
    resourceRegionGetInfoRequest = Maincity.ResourceRegionGetInfoRequest(resourceRegionGetInfoCheck, resourceRegionGetInfoCallback, 0, 0)
    politicalEventGetInfoRequest = Maincity.PoliticalEventGetInfoRequest(politicalEventGetInfoCheck, politicalEventGetInfoCallback, 0, 0)
    maincityManager.startThread()
        
def pause():
    global maincityManager
    if maincityManager != None:
        maincityManager.pauseThread()
        
class MaincityManager(BasicThread.BasicThread):
    
    def callback(self):
        time.sleep(20)
        #检查功能是否开放
        if FunctionData.func4 == 0:
            return
        
        #获取主城信息
        global maincityGetInfoRequest
        if maincityGetInfoRequest.run():
            return
        
        #获取资源区信息
        global resourceRegionGetInfoRequest
        if resourceRegionGetInfoRequest.run():
            return
        
        #获取政务事件信息
        global politicalEventGetInfoRequest
        if politicalEventGetInfoRequest.run():
            return